using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class BadgeMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.bg_Menu, new Point(0, -107));
	private static bool isMenuInitialized;

	private static List<dynamic> badgeList = new List<object>(new string[4] { "1", "2", "3", "4" });

	private static UIMenuListItem badgeItem;

	private static int gid;

	public BadgeMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.OnListChange += OnListChange;
			menu.OnMenuClose += OnMenuClose;
			menu.OnItemSelect += OnItemSelect;
			isMenuInitialized = true;
		}
	}

	public static void InitBadgeMenu()
	{
		menu.Clear();
		menu.SetBannerType(new UIResRectangle());
		badgeItem = new UIMenuListItem(TextSetting.tb_ListItem_Group, badgeList, gid);
		menu.AddItem(badgeItem);
		menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_Torso} {(object)badgeList[badgeItem.Index]}", TextTipsChange(0, gid)));
		menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_Head} {(object)badgeList[badgeItem.Index]}", TextTipsChange(1, gid)));
		menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_LeftArm} {(object)badgeList[badgeItem.Index]}", TextTipsChange(2, gid)));
		menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_RightArm} {(object)badgeList[badgeItem.Index]}", TextTipsChange(3, gid)));
		menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_LeftLeg} {(object)badgeList[badgeItem.Index]}", TextTipsChange(4, gid)));
		menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_RightLeg} {(object)badgeList[badgeItem.Index]}", TextTipsChange(5, gid)));
		menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_ClearGroup} {(object)badgeList[badgeItem.Index]}"));
		for (int i = 1; i < 7; i++)
		{
			menu.BindMenuToItem(BadgeSubMenu.menu, menu.MenuItems[i]);
		}
		menu.RefreshIndex();
		CheckCanUseMenuItem();
	}

	public static void CheckCanUseMenuItem()
	{
		int[] array = new int[6];
		if (DressingScript.Dressing == Main.Member.P2)
		{
			if (Main.P2.skin.BadgeData == null)
			{
				Main.P2.skin.GetDecorationData(Decoration.Type.TYPE_BADGE);
			}
			for (int i = 0; i < Main.P2.skin.BadgeData.Length; i++)
			{
				for (int j = 0; j < Main.P2.skin.BadgeData[i].Count; j++)
				{
					if (Main.P2.skin.BadgeData[i][j].nameHash != string.Empty)
					{
						array[i]++;
					}
				}
			}
		}
		else
		{
			if (Main.P1.skin.BadgeData == null)
			{
				Main.P1.skin.GetDecorationData(Decoration.Type.TYPE_BADGE);
			}
			for (int k = 0; k < Main.P1.skin.BadgeData.Length; k++)
			{
				for (int l = 0; l < Main.P1.skin.BadgeData[k].Count; l++)
				{
					if (Main.P1.skin.BadgeData[k][l].nameHash != string.Empty)
					{
						array[k]++;
					}
				}
			}
		}
		for (int m = 1; m < 7; m++)
		{
			menu.MenuItems[m].Enabled = false;
		}
		for (int n = 1; n < 7; n++)
		{
			if (array[n - 1] != 0)
			{
				menu.MenuItems[n].Enabled = true;
			}
		}
	}

	private void OnListChange(UIMenu sender, UIMenuListItem list, int index)
	{
		gid = badgeItem.Index;
		InitBadgeMenu();
	}

	private static string TextTipsChange(int zid, int gid)
	{
		string result = TextSetting.tb_TextTips_NULL;
		if (DressingScript.Dressing == Main.Member.P2)
		{
			if (Main.P2.skin.Badge[zid][gid].action)
			{
				result = TextSetting.tb_TextTips_InUse;
			}
		}
		else if (Main.P1.skin.Badge[zid][gid].action)
		{
			result = TextSetting.tb_TextTips_InUse;
		}
		return result;
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		if (index >= 1 && index < 7)
		{
			BadgeSubMenu.InitBadgeSubMenu(index - 1, gid);
		}
		if (index == 7)
		{
			if (DressingScript.Dressing == Main.Member.P2)
			{
				Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid);
			}
			else
			{
				Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid);
				Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid);
			}
			InitBadgeMenu();
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
		if (index >= 1 && index < 7)
		{
			TextTipsChange(index - 1, gid);
		}
		InitBadgeMenu();
		sender.CurrentSelection = index;
	}

	private void OnMenuClose(UIMenu sender)
	{
		menu.Visible = false;
		menu.RefreshIndex();
	}
}
